package com.example.mywebrtc;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoTrack;

import java.util.HashMap;
import java.util.Map;

public class WebRtcSurfaceViewRenderer extends LinearLayout {
    private SurfaceViewRenderer mySurface;
    private LinearLayout llSurface;
    private OnSurfaceInitFinish onSurfaceInitFinish;
    private Map<String, View> childViewMap;

    public void setOnSurfaceInitFinish(OnSurfaceInitFinish onSurfaceInitFinish) {
        this.onSurfaceInitFinish = onSurfaceInitFinish;
    }

    public interface OnSurfaceInitFinish{
        void onFinishSelf();
        void OnChildFinish(SurfaceViewRenderer renderer, VideoTrack videoTrack);
    }
    public WebRtcSurfaceViewRenderer(Context context) {
        super(context);
        init();
    }

    public WebRtcSurfaceViewRenderer(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WebRtcSurfaceViewRenderer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public WebRtcSurfaceViewRenderer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        View view = View.inflate(getContext(),R.layout.video_surface,null);
        addView(view);

        childViewMap = new HashMap<>();

        post(new Runnable() {
            @Override
            public void run() {
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.width = getWidth();
                layoutParams.height = getHeight();
                if (onSurfaceInitFinish != null) {
                    onSurfaceInitFinish.onFinishSelf();
                }
            }
        });
        mySurface = findViewById(R.id.surface_my);
        llSurface = findViewById(R.id.ll_add);
    }

    public SurfaceViewRenderer SelfViewRenderer(){
        return mySurface;
    }

    public void createSurface(String socketId, VideoTrack videoTrack) {
        View view = View.inflate(getContext(),R.layout.surface_view_child,null);
        SurfaceViewRenderer viewRenderer = view.findViewById(R.id.view_renderer);
        llSurface.addView(view);
        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
        layoutParams.width = (int) (llSurface.getHeight() / 1.5);
        layoutParams.height = llSurface.getHeight();
        view.post(new Runnable() {
            @Override
            public void run() {
                if (onSurfaceInitFinish != null) {
                    childViewMap.put(socketId, view);
                    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M && childViewMap.size() == 1) {
                        ViewGroup.LayoutParams layoutParams1 = mySurface.getLayoutParams();
                        layoutParams1.height = mySurface.getHeight() - llSurface.getHeight();
                    }
                    onSurfaceInitFinish.OnChildFinish(viewRenderer, videoTrack);
                }
            }
        });
    }

    public void removeSurface(String socketId) {
        View view = childViewMap.get(socketId);
        llSurface.removeView(view);
    }
}
